//  代码地址: https://github.com/CoderJsMjLee/JsMjRefresh
//  代码地址: http://code4app.com/ios/%E5%BF%AB%E9%80%9F%E9%9B%86%E6%88%90%E4%B8%8B%E6%8B%89%E4%B8%8A%E6%8B%89%E5%88%B7%E6%96%B0/52326ce26803fabc46000000
//  UIScrollView+JsMjRefresh.m
//  JsMjRefreshExample
//
//  Created by JsMj Lee on 15/3/4.
//  Copyright (c) 2015年 小码哥. All rights reserved.
//

#import "UIScrollView+JsMjRefresh.h"
#import "JsMjRefreshHeader.h"
#import "JsMjRefreshFooter.h"
#import "JsMjRefreshTrailer.h"
#import <objc/runtime.h>

@implementation UIScrollView (JsMjRefresh)

#pragma mark - header
static const char JsMjRefreshHeaderKey = '\0';
- (void)setJsMj_header:(JsMjRefreshHeader *)jsMj_header
{
    if (jsMj_header != self.jsMj_header) {
        // 删除旧的，添加新的
        [self.jsMj_header removeFromSuperview];
        [self insertSubview:jsMj_header atIndex:0];
        
        // 存储新的
        objc_setAssociatedObject(self, &JsMjRefreshHeaderKey,
                                 jsMj_header, OBJC_ASSOCIATION_RETAIN);
    }
}

- (JsMjRefreshHeader *)jsMj_header
{
    return objc_getAssociatedObject(self, &JsMjRefreshHeaderKey);
}

#pragma mark - footer
static const char JsMjRefreshFooterKey = '\0';
- (void)setJsMj_footer:(JsMjRefreshFooter *)jsMj_footer
{
    if (jsMj_footer != self.jsMj_footer) {
        // 删除旧的，添加新的
        [self.jsMj_footer removeFromSuperview];
        [self insertSubview:jsMj_footer atIndex:0];
        
        // 存储新的
        objc_setAssociatedObject(self, &JsMjRefreshFooterKey,
                                 jsMj_footer, OBJC_ASSOCIATION_RETAIN);
    }
}

- (JsMjRefreshFooter *)jsMj_footer
{
    return objc_getAssociatedObject(self, &JsMjRefreshFooterKey);
}

#pragma mark - footer
static const char JsMjRefreshTrailerKey = '\0';
- (void)setMj_trailer:(JsMjRefreshTrailer *)jsMj_trailer {
    if (jsMj_trailer != self.jsMj_trailer) {
        // 删除旧的，添加新的
        [self.jsMj_trailer removeFromSuperview];
        [self insertSubview:jsMj_trailer atIndex:0];
        
        // 存储新的
        objc_setAssociatedObject(self, &JsMjRefreshTrailerKey,
                                 jsMj_trailer, OBJC_ASSOCIATION_RETAIN);
    }
}

- (JsMjRefreshTrailer *)jsMj_trailer {
    return objc_getAssociatedObject(self, &JsMjRefreshTrailerKey);
}

#pragma mark - 过期
- (void)setFooter:(JsMjRefreshFooter *)footer
{
    self.jsMj_footer = footer;
}

- (JsMjRefreshFooter *)footer
{
    return self.jsMj_footer;
}

- (void)setHeader:(JsMjRefreshHeader *)header
{
    self.jsMj_header = header;
}

- (JsMjRefreshHeader *)header
{
    return self.jsMj_header;
}

#pragma mark - other
- (NSInteger)jsMj_totalDataCount
{
    NSInteger totalCount = 0;
    if ([self isKindOfClass:[UITableView class]]) {
        UITableView *tableView = (UITableView *)self;

        for (NSInteger section = 0; section < tableView.numberOfSections; section++) {
            totalCount += [tableView numberOfRowsInSection:section];
        }
    } else if ([self isKindOfClass:[UICollectionView class]]) {
        UICollectionView *collectionView = (UICollectionView *)self;

        for (NSInteger section = 0; section < collectionView.numberOfSections; section++) {
            totalCount += [collectionView numberOfItemsInSection:section];
        }
    }
    return totalCount;
}

@end
